package com.shanghaionstar.nav;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;

import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.common.util.Constants;
import com.onstar.cn.framework.annotation.ContentView;
import com.onstar.cn.framework.mvp.base.core.BaseFragment;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.framework.utils.RxBus;
import com.onstar.cn.mag.enumtype.RemoteCommandsType;
import com.onstar.cn.map.common.LocationType;
import com.onstar.cn.map.common.PoiItemInfo;
import com.onstar.cn.map.core.impl.OGeoPointImpl;
import com.onstar.cn.rxconstants.RxConstants;
import com.onstar.cn.vehicle.result.PollingResult;
import com.shanghaionstar.R;
import com.shanghaionstar.nav.service.AbstractMapActivity;
import com.shanghaionstar.utils.FinalConstants;
import com.shanghaionstar.utils.MapHandler;
import com.shanghaionstar.utils.NavHolder;

/**
 * Created by 6335yz on 2016/7/7.
 */
@ContentView(R.layout.fragment_container)
public class NavActivity extends AbstractMapActivity {
    private NavMapFragment navMapFragment;
    private NavMainFragment navMainFragment;

    @Override
    public void initMapHandler(MapHandler mapHandler) {
        this.mapHandler = mapHandler;
    }

    @Override
    public BasePresenter getPresenter() {
        return null;
    }

    /**
     * 地图和searchmain同时加载，以showhide形式交互
     *
     * @param savedInstanceState
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        navMapFragment = new NavMapFragment();
        BaseFragment baseFragment = initInputCommand();
        addFragment(getSupportFragmentManager(), navMapFragment, true);
        addFragment(getSupportFragmentManager(), navMainFragment, true);
        showFragment(getSupportFragmentManager(), baseFragment.getClass(), null);
//        /**
//         * 第一次进入map如果有车辆位置信息，则显示在地图
//         */
//        boolean isShowToMap = getIntent().getBooleanExtra("isShowToMap", false);
//        if (isShowToMap) {
//            if (NavHolder.getInstance(this).getVehicleLocation() != null) {
//                showFragment(getSupportFragmentManager(), NavMapFragment.class, NavMainFragment.class);
//                NavHolder.getInstance(this).setShowMap(true);
//                RxBus.getDefault().post(RxConstants.VEHICLE_LOCATE, getIntent().getSerializableExtra("pollingResult"));
//            }
//        }
        sendAmapPoiToCar(getIntent());
    }

    /**
     * 发送高德poi到车
     */
    private void sendAmapPoiToCar(Intent intent) {
        log.d("sendAmapPoiToCar");
        if (intent == null)
            return;
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            String share3RDFlag = bundle.getString(Constants.PARTNER_3RD_FLAG);
            if ("sharePOI".equals(share3RDFlag) && navMainFragment != null) {
                navMainFragment.setArgument(bundle);
            }
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        /**
         * 点击显示车辆位置
         */
        super.onNewIntent(intent);
        setIntent(intent);

        sendAmapPoiToCar(intent);

        boolean isShowToMap = intent.getBooleanExtra("isShowToMap", false);
        PollingResult pollingResult = (PollingResult) intent.getSerializableExtra("pollingResult");
        if (isShowToMap && pollingResult != null) {
            log.d("NavActivity pollingResult" + pollingResult.getRequestType());
            showFragment(getSupportFragmentManager(), NavMapFragment.class, NavMainFragment.class);
            NavHolder.getInstance(this).setShowMap(true);
            RxBus.getDefault().post(RxConstants.VEHICLE_LOCATE, pollingResult);
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        /**
         * 第一次进入map如果有车辆位置信息，则显示在地图
         */
        boolean isShowToMap = getIntent().getBooleanExtra("isShowToMap", false);
        if (isShowToMap) {
            if (NavHolder.getInstance(this).getVehicleLocation() != null) {
                showFragment(getSupportFragmentManager(), NavMapFragment.class, NavMainFragment.class);
                NavHolder.getInstance(this).setShowMap(true);
                RxBus.getDefault().post(RxConstants.VEHICLE_LOCATE, getIntent().getSerializableExtra("pollingResult"));
            }
        }
    }

    @Override
    public void onBackPressed() {
        FragmentManager fragmentManager = getSupportFragmentManager();
        log.d("onBackPressed current fragment tag is----->>" + getCurrentFragmentTag());
        String currentTag = getCurrentFragmentTag();
        BaseFragment current = (BaseFragment) (fragmentManager.findFragmentByTag(currentTag));
        if (current != null) {
            current.onFragmentBackPress();
        }
    }

    /**
     * 地图点的图标
     *
     * @return
     */
    @Override
    public int getmMyLocationArrow() {
        return R.drawable.nav_map_marker_my;
    }

    /**
     * Vehicle location
     */
    @Override
    public int getmVehicleLocationArrow() {
        return R.drawable.nav_map_marker_vehicle;
    }

    /**
     * POI location
     */
    @Override
    public int getmPoiLocationArrow() {
        return R.drawable.nav_map_marker_poi;
    }

    @Override
    public int getmFootPrintArror() {
        return R.drawable.nav_foot_print_marker;
//        return R.drawable.onstar_geofence_preview_icon;
    }


    /**
     * 用于从其他页面跳转到map页面；
     * 地图栏，用来显示导航，我的详情等快捷信息
     *
     * @param isShow
     */
    public void showBottomView(PoiItemInfo poiItemInfo, boolean isShow) {
        navMapFragment = (NavMapFragment) getSupportFragmentManager().findFragmentByTag(NavMapFragment.class.getSimpleName());
        if (navMapFragment != null) {
            navMapFragment.showBottomView(poiItemInfo, isShow);
        }
    }

    @Override
    public void showPoiToMap(PoiItemInfo poiItemInfo, LocationType locationType) {
        if (mapHandler != null) {
            switch (locationType) {
                case POI:
                case VEHICLE:
                case MYSELF:
                    /**
                     * 共有三种poi信息，其他两种记录-- 我的位置，和车辆位置,在Maphandler中得到poi后赋值
                     */
                    if (locationType == LocationType.POI) {
                        getNavDataHolder().setSearchPoi(poiItemInfo);
                    }
                    mapHandler.showLocation(locationType, poiItemInfo);
                    mapHandler.setMapPoi_PoiItemInfo(poiItemInfo);
                    if (poiItemInfo != null) {
                        NavHolder.getInstance(this).setGeoPointImpl(new OGeoPointImpl(poiItemInfo.getLatitude(), poiItemInfo.getLongitude()));
                    }
                    getNavDataHolder().setCurrentMapPoi(poiItemInfo);
                    removeUntilFragment(getSupportFragmentManager(), NavMainFragment.class, false);
                    showFragment(getSupportFragmentManager(), NavMapFragment.class, getClass());
                    showBottomView(poiItemInfo, true);
                    break;
                case FOOTPRINT:
                    mapHandler.showLocation(locationType, poiItemInfo);
                    break;
            }
        }
    }

    private BaseFragment initInputCommand() {
        if (getIntent() != null) {
            Bundle bundle = getIntent().getExtras();
            if (bundle != null) {
                String title = bundle.getString("title");
                String jumpFlag = bundle.getString(FinalConstants.JUMP_FLAG);
                if (!CommonUtil.isNull(title)) {
                    String[] poiStrList = getResources().getStringArray(R.array.poi_list);
                    String[] servicesStrList = getResources().getStringArray(R.array.vehicle_sevices_list);
                    if (poiStrList[2].equals(title)) {//一键回家
                        navMainFragment = NavMainFragment.newInstance(RemoteCommandsType.COMMAND_GO_HOME);
                        return navMainFragment;
                    } else if (servicesStrList[0].equals(title)) {//车辆位置
                        navMainFragment = NavMainFragment.newInstance(RemoteCommandsType.COMMANDS_LOCATION);
                        return navMainFragment;
                    } else if (poiStrList[3].equals(title)) {//回公司
                        navMainFragment = NavMainFragment.newInstance(RemoteCommandsType.COMMAND_GO_COMPANY);
                        return navMainFragment;
                    } else if (poiStrList[0].equals(title)) {
                        navMapFragment = NavMapFragment.newInstance(null, null);
                        navMainFragment = new NavMainFragment();
                        return navMapFragment;
                    } else {
                        navMainFragment = new NavMainFragment();
                        return navMainFragment;
                    }
                } else if (!CommonUtil.isNull(jumpFlag) && jumpFlag.equals(NavMapFragment.class.getSimpleName())) {
                    navMapFragment = NavMapFragment.newInstance((PoiItemInfo) bundle.getSerializable("poiItemInfo"), bundle.getString(FinalConstants.FROM_PAGE));
                    navMainFragment = new NavMainFragment();
                    return navMapFragment;
                } else {
                    navMainFragment = new NavMainFragment();
                    return navMainFragment;
                }
            } else {
                navMainFragment = new NavMainFragment();
                return navMainFragment;
            }
        } else {
            navMainFragment = new NavMainFragment();
            return navMainFragment;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 1) {
            getCurrentFragment(getSupportFragmentManager()).onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
